<template>
  <div class="login"
       style="height: 100vh;  display: flex; align-items: center; justify-content: center; background-image: url('https://www.dmoe.cc/random.php'); background-size: cover ">
    <div style="margin-right: -700px; background: rgba(255, 255, 255, 0.6); /* 半透明背景 */margin-right: 100px; border-radius: 20px; ">
      <div style="display: flex; width: 450px; height: 400px; align-items: center; justify-content: center;">
        <el-form :model="user" :rules="rules" style="width: 70%;" ref="loginRef">
          <div style="font-size: 20px; font-weight: bold; margin-bottom: 20px; text-align: center;">欢迎登录</div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input show-password prefix-icon="el-icon-s-promotion" placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div style="display: flex;">
              <el-input prefix-icon="el-icon-message-solid" style="flex: 1; margin-bottom: 10px;" placeholder="请输入验证码"
                        v-model="user.code"></el-input>
              <div style="flex: 1; width: 50px; height: 36px;">
                <valid-code @input="getCode"/>
              </div>
            </div>
          </el-form-item>
          <el-from-itme>
            <el-button style="width: 100%;" type="primary" @click="login11">登录</el-button>
          </el-from-itme>
          <div style="display: flex;">
            <div style="font-size: 14px; flex: 1; margin-top: 10px;">还没有账号？请 <span
                style="color: rgb(55, 222, 222)" @click="$router.push('/register')">注册</span></div>
            <div style="flex: 1; color: rgb(55, 222, 222); text-align: right; margin-top: 10px;">
              <strong @click="handleForgetPassword">忘记密码</strong>
            </div>
          </div>
        </el-form>
      </div>
    </div>

    <el-dialog title="忘记密码" :visible.sync="forgetPassWord" width="30%">
      <el-form :model="forgetPasswordFrom" :rules="rules01" label-width="80px" style="padding-right: 20px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="forgetPasswordFrom.username" autocomplete="off" placeholder="请输入用户名"></el-input>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input show-password v-model="forgetPasswordFrom.phone" autocomplete="off" placeholder="请输入手机号"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="forgetPassWord = false">取 消</el-button>
        <el-button type="primary" @click="resetPassword">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import ValidCode from "../components/ValidCode.vue";
import request from "@/utils/request";
export default {
  name: "Login",
  components: {
    ValidCode
  },
  data() {
    var validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if(value.toLowerCase() !== this.code) {
        callback(new Error('验证码不正确'))
      } else {
        callback();
      }
    };
    return {
      forgetPassWord: false,
      forgetPasswordFrom: {
        username: '',
        phone: ''
      },
      user: {
        username: '',
        password: '',
        code: ''
      },
      code: '',
      register: '',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        code: [
          { validator: validateCode, trigger: 'blur' }
        ]
      },

      rules01: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
        ]
      }
    }
  },
  methods: {

    handleForgetPassword() {
    //  初始化页面
      this.forgetPasswordFrom = {},
      this.forgetPassWord = true
    },

    resetPassword() {
      this.$confirm('确定要重置密码吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //触发更新
        request.post('/user/forget', this.forgetPasswordFrom).then(res => {
          if (res.code === 200) {
            this.$message.success('修改成功,请重新登陆')
            this.forgetPassWord = false
            this.$router.push('/')
          } else {
            // 登录失败
            this.$message.error(res.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消重置密码'
        });
      });
    },
    getCode(code) {
      console.log(code)
      this.code = code.toLowerCase()
    },
    login11() {
      this.$refs['loginRef'].validate(valid => {
        if (valid) {
          // 验证通过发起请求
          request.post('/login/login', this.user).then(res => {
            if (res.code === 200) {
              // 登录成功
              // 1. 保存token
              localStorage.setItem('token', res.data.token)
              // 2. 跳转到首页
              this.$router.push('/First')
              this.$message.success('登陆成功')
              localStorage.setItem("user11", JSON.stringify(res.data))  //存储用户数据
            } else {
              // 登录失败
              this.$message.error(res.message)
            }
          })
        }
      })
    },
  }
}
</script>

<style scoped>

</style>